package it.unibo.convoycruisecontrol.chiefboatsubsystem.test.unit;

import static org.junit.Assert.*;

import java.util.Random;

import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocation;
import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocationDevice;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.test.mock.MockNavigationInfoReceiver;
import it.unibo.location.ILocation;
import it.unibo.util.information.InformationConverter;
import it.unibo.util.information.PrologInformationConverter;

import org.junit.BeforeClass;
import org.junit.Test;

public class NavigationInfoReceiverTestCase 
{
	static MockNavigationInfoReceiver receiver;
	static InformationConverter infoConverter;
	static int boatsNumber = 3;
	static float[] expectedSpeeds;
	static ILocation[] expectedLocations;
	static String[] messages;

	@BeforeClass
	public static void beforeClass()
	{
		try
		{
			infoConverter = new PrologInformationConverter();
			receiver = new MockNavigationInfoReceiver(boatsNumber, new MockLocationDevice(), infoConverter);

			expectedSpeeds = new float[boatsNumber];
			expectedLocations = new ILocation[boatsNumber];
			messages = new String[boatsNumber];
			Random rand = new Random();
			for(int i = 0; i < boatsNumber; i++)
			{
				expectedSpeeds[i] = rand.nextFloat();
				expectedLocations[i] = new MockLocation(rand.nextFloat(), rand.nextFloat(), 0);
				messages[i] = "boatInfo([],speed("+ expectedSpeeds[i] +"),longitude(" + expectedLocations[i].getLongitude() + ")"+
						",latitude(" + expectedLocations[i].getLatitude() + "),sequenceNumber(" + i + "))";
			}

		}
		catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void testExtractInfo() 
	{	
		try
		{
			for(int i = 0; i < boatsNumber; i++)
			{
				receiver.extractInfo(messages[i]);
				assertEquals(expectedSpeeds[i], receiver.getSpeeds()[i], 0.0);
				assertEquals(expectedLocations[i], receiver.getLocations()[i]);
				assertEquals(i, receiver.getLastInfoReceived());
			}
			
			assertArrayEquals(expectedSpeeds, receiver.getSpeeds(), 0.0f);
			assertArrayEquals(expectedLocations, receiver.getLocations());
		}
		catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testWrongMessage() throws Exception 
	{
		receiver.extractInfo("info(");
	}

}
